<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use common\helpers\TStringHelper;

/* @var $this yii\web\View */
/* @var $model common\models\System */
/* @var $form yii\widgets\ActiveForm */
?>
<?=Html::jsFile('/static/common/js/ajaxfileupload.js')?>
<div class="system-form">
    <?php $form = ActiveForm::begin(); ?>
    <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
    <?= $form->field($model, 'code')->textInput(['maxlength' => true]) ?>
    <?= $form->field($model, 'value')->textInput(['id' => 'litpic'])->hint('可以输入值或者上传图片文件生成地址') ?>
    <input type="file" name="Filedata" class="file_upload_attach" id="upload_child_attach" data-id="attach">

    <?= $form->field($model, 'order')->textInput(['type' => 'number']) ?>
    <?= $form->field($model, 'status')->dropDownList(TStringHelper::getStatusText()) ?>
    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? Yii::t('common', 'create') : Yii::t('common', 'update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>
<script>
    $(function () {
        //此处用了change事件，当选择好图片打开，关闭窗口时触发此事件
        $("#upload_child_attach").change(function (e) {
            var val = e.target.value;
            var point = val.lastIndexOf(".");
            var type = val.substr(point).toLowerCase();
            if (type != ".jpg" && type != ".jpeg" && type != ".png" && type != ".mp4") {
                alert('系统目前只支持.jpg、.jpeg、.png、.mp4文件格式');
                return false;
            }
            $.ajaxFileUpload({
                //处理图片的脚本路径
                url: '<?=\yii\helpers\Url::toRoute(['common/upload-thumb', 'event' => 1, 'dir' => 'system'])?>',
                type: 'post',       //提交的方式
                secureuri: false,   //是否启用安全提交
                fileElementId: 'upload_child_attach',     //file控件ID
                dataType: 'json',  //服务器返回的数据类型
                success: function (data, status) {  //提交成功后自动执行的处理函数
                    if (data.error) {
                        alert(data.msg);
                        return false;
                    } else {
                        $("input[id='litpic']").val(data.fileUrl);
                    }
                },
                error: function (data, status, e) {   //提交失败自动执行的处理函数
                    alert(e);
                }
            })
        })
    })
</script>